#pragma once
#include <vector>
#include <string>
#include <set>
#include "Utility.h"

namespace bs
{
	class IMenuListener
	{
	public:
		virtual void OnClickItem(class Menu* sender, int cmd) = 0;
	};

	class Menu
	{
	public:
		Menu();
		~Menu();

		void AttachListener(IMenuListener* listener) { this->listeners.insert(listener); }
		void DetachListener(IMenuListener* listener) { this->listeners.erase(listener); }

		bool InsertItem(const char* name, int cmd, int pos = -1, bool end = false);
		void DeleteItem(int from, int to);
		//bool Load(const char* items);    // items separate by ";", such as "start;intro;end"
		void ClearItems();

		void Update(bool lBtnDown, bool rBtnDown, Point cursor);
		void Draw(HDC dc);

		void PopUp(int x, int y) { this->show = true; this->x = x; this->y = y; }

		void Show(bool show) { this->show = show; }
		void SetPos(int x, int y) { this->x = x; this->y = y; }
		void SetAutoHide(bool isAuto) { this->autoHide = isAuto; }

		// the follows should be called before the InsertItem()s and Load()
		void SetPreIndent(int indent) { this->preIndent = indent; }
		void SetPostIndent(int indent) { this->postIndent = indent; }
		void SetSpace(int space) { this->space = space; }
		void SetAlign(bool left) { this->alignLeft = left; }    // if false, then will be center
		void SetBkColor(COLORREF color);
		void SetSelColor(COLORREF color);
		void SetTextColor(COLORREF color) { this->textColor = color; }
		void SetFontFace(const char* face);
		void SetFontSize(int size);

		int GetWidth() { return this->width; }
		int GetHeight() { return this->height; }

	private:
		void CreateMenu();
		inline void DrawBack();
		void DrawText();
		void DrawSel(HDC dc);

		void CreateObjects();
		void DeleteObjects();

	private:
		int x;
		int y;
		int width;
		int height;
		int textWidth;
		int textHeight;
		int preIndent;
		int postIndent;
		int space;
		bool alignLeft;

		bool created;
		bool show;
		int hoverItem;

		bool autoHide;

		std::set<IMenuListener*> listeners;

		HDC memDc;
		HBITMAP memBmp;
		HBITMAP oldBmp;
		COLORREF textColor;
		HBRUSH bkBrush;
		HBRUSH selBrush;
		LOGFONT logFont;
		HFONT font;
		HFONT oldFont;

		std::vector<std::string> items;
		std::vector<int> cmds;
	};

}
